/**
RoomBot, a IRC bot.
Copyright (C) 2009 Joshua Lückers

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.googlecode.roombot.util;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * The config handler. This class makes it possible to get the settings from the
 * config file (config.xml).
 * @author Joshua Lückers
 * @version 08-11-2009
 */
public class ConfigHandler {

    private static ConfigHandler configHandler;
    private File configFile;
    private String mysqlServer, mysqlUser, mysqlPasswd, mysqlDb;

    static {
        configHandler = new ConfigHandler();
    }

    /**
     * Parse the items from the configuration file.
     */
    public void getConfiguration() {
        try {
            configFile = new File("config.xml");

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();

            Document doc = db.parse(configFile);
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("config");

            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);

                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element nElement = (Element) node;
                    Element cElement;

                    cElement = (Element) nElement.getElementsByTagName("server").item(0);
                    mysqlServer = cElement.getChildNodes().item(0).getNodeValue();

                    cElement = (Element) nElement.getElementsByTagName("user").item(0);
                    mysqlUser = cElement.getChildNodes().item(0).getNodeValue();

                    cElement = (Element) nElement.getElementsByTagName("passwd").item(0);
                    mysqlPasswd = cElement.getChildNodes().item(0).getNodeValue();

                    cElement = (Element) nElement.getElementsByTagName("db").item(0);
                    mysqlDb = cElement.getChildNodes().item(0).getNodeValue();
                }
            }
        } catch (Exception e) {
            e.getStackTrace();
        }
    }

    /**
     * The MySql server to connect to.
     * @return String mysqlServer
     */
    public String getMysqlServer() {
        return mysqlServer;
    }

    /**
     * Get the MySql user.
     * @return String mysqlUser
     */
    public String getMysqlUser() {
        return mysqlUser;
    }

    /**
     * Get the mysql user password.
     * @return String mysqlPasswd
     */
    public String getMysqlPasswd() {
        return mysqlPasswd;
    }

    public String getMysqlDB() {
        return mysqlDb;
    }

    /**
     * Get the ConfigHandler instance.
     * @return ConfigHandler configHandler
     */
    public static ConfigHandler getInstance() {
        return configHandler;
    }
}
